package com.sxran.im.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sxran.im.dao.ImConversationMapper;
import com.sxran.im.info.ImChatBody;
import com.sxran.im.info.ImConversation;
import com.sxran.im.info.ImGroup;
import com.sxran.im.info.ImUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@Service
public class ConversationService {
    @Resource
    private ImConversationMapper imConversationMapper;

    @Resource
    private UserService userService;
    @Resource
    private GroupService groupService;

    public void clearUnreadCount(String userId,String conversationId){
        LambdaQueryWrapper<ImConversation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ImConversation::getUserId,userId);
        queryWrapper.eq(ImConversation::getId,conversationId);
        ImConversation conversation = new ImConversation();
        conversation.setUnreadCount(0);
        imConversationMapper.update(conversation,queryWrapper);
    }

    public void updateOrInsert(String userId,String conversationId,String msg,Integer type,String to,int unreadCount){
        LambdaQueryWrapper<ImConversation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ImConversation::getUserId,userId);
        queryWrapper.eq(ImConversation::getId,conversationId);

        ImConversation conversation = imConversationMapper.selectOne(queryWrapper);

        if (conversation == null) {
            conversation = new ImConversation();
            conversation.setId(conversationId);
            conversation.setUserId(userId);
            conversation.setLastMsgNote(msg);
            fillConversation(type, to, conversation);
            conversation.setUnreadCount(unreadCount);
            conversation.setUpdateTime(new Date());
            imConversationMapper.insert(conversation);
        }else{
            conversation.setUnreadCount(conversation.getUnreadCount()+unreadCount);
            conversation.setUpdateTime(new Date());
            conversation.setLastMsgNote(msg);
            imConversationMapper.update(conversation,queryWrapper);
        }

    }

    /**
     * 填充conversation
     * @param type
     * @param to
     * @param conversation
     */
    public void fillConversation(Integer type, String to, ImConversation conversation) {
        if(type == 1){
            ImGroup imGroup = groupService.selectByGroupId(to);
            conversation.setTitle(imGroup.getName());
            conversation.setAvatar(imGroup.getAvatar());
            conversation.setGroupId(to);
        }else if (type == 2){
            ImUser imUser = userService.selectByUserId(to);
            conversation.setTitle(imUser.getNick());
            conversation.setAvatar(imUser.getAvatar());
            conversation.setFriendUid(to);
        }
        conversation.setType(type);
        conversation.setUnreadCount(0);
    }
}
